package com.longdaji.decoration.ui.activitiesOfMine.refund.refundDetail;

import android.text.TextUtils;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.bean.RefundGoods;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.RxUtil;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/4/10 0010.
 */

public class RefundDetailPresenter extends RxPresenter<RefundDetailContract.IView> implements RefundDetailContract.IPresenter {

    private DataManager mDataManager;

    @Inject
    public RefundDetailPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }

//    @Override
//    public void initData(String orderNo) {
//        addSubscribe(mDataManager.doGetOrderApi(Account.getInstance().getUserid(), null, orderNo)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(RxUtil.<Orders>handleResult())
//                .subscribeWith(new CommonSubscriber<Orders>() {
//                    @Override
//                    public void onSuccess(Orders orders) {
//                        LogUtil.d("succeed : doGetOrder ");
//                        mView.showRefundDetail(orders.getOrders().getOrder().get(0));
//                    }
//
//                    @Override
//                    public void onFailed(String msg) {
//                        LogUtil.d("failed : " + msg);
//
//                    }
//                }));
//    }


    @Override
    public void initData(final String orderNo, final int goodsId) {
//        addSubscribe(mDataManager.doGetRefundGoodsApi(Account.getInstance().getUserid())
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(RxUtil.<RefundGoods>handleResult())
//                .subscribeWith(new CommonSubscriber<RefundGoods>() {
//                    @Override
//                    public void onSuccess(RefundGoods refundGoods) {
//                        LogUtil.i("success doGetRefundGoodsApi: " + refundGoods.getRefundProduct().size());
//                        List<RefundGoods.RefundProductBean> refundProductList = refundGoods.getRefundProduct();
//                        for (RefundGoods.RefundProductBean refundProduct : refundProductList) {
//                            if (TextUtils.equals(refundProduct.getOrderNo(), orderNo) && refundProduct.getGoodsId() == goodsId) {
//                                mView.showRefundProduct(refundProduct);
//                                return;
//                            }
//                        }
//                        LogUtil.e("refundtest", "无此商品");
//
//                    }
//
//                    @Override
//                    public void onFailed(String msg) {
//                        LogUtil.i("fail doGetRefundGoodsApi: " + msg);
//
//                    }
//                }));

        addSubscribe(mDataManager.doGetRefundGoodsApi(Account.getInstance().getUserid())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<List<RefundGoods>>handleResult())
                .subscribeWith(new CommonSubscriber<List<RefundGoods>>() {
                    @Override
                    public void onSuccess(List<RefundGoods> refundGoodsList) {
                        LogUtil.i("mytest", "success doGetRefundGoodsApi");
                        for (RefundGoods refundGoods : refundGoodsList) {
                            if (TextUtils.equals(refundGoods.getOrderNo(), orderNo) && refundGoods.getGoodsId() == goodsId) {
                                mView.showRefundProduct(refundGoods);
                                return;
                            }
                        }
                        LogUtil.e("refundtest", "无此商品");

                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.i("mytest", "fail doGetRefundGoodsApi:" + msg);
                    }
                }));
    }

}
